package com.tianyang.yukuaifuzs.business.trade.presenter;

import com.tianyang.yukuaifuzs.business.trade.bean.SubAgentStatOutput;
import com.tianyang.yukuaifuzs.business.trade.contract.SubAgentStatContract;
import com.tianyang.yukuaifuzs.business.trade.model.TradeModel;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：间推业绩详情
 * Created by xyj on 2024/3/25
 */
public class SubAgentStatPresenter implements SubAgentStatContract.Presenter {

    private SubAgentStatContract.View mView;

    private CompositeSubscription mSubscription;

    private String agentName, agentPhone;

    public SubAgentStatPresenter(SubAgentStatContract.View mView, String agentName, String agentPhone) {
        this.mView = mView;
        this.agentName = agentName;
        this.agentPhone = agentPhone;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void subAgentStatHistoryDetail(String startTime, String endTime) {
        mView.showLoadingDialog(false);
        Subscription subscription = TradeModel.getInstance()
                .subAgentStatHistoryDetail(startTime, endTime, agentName, agentPhone)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SubAgentStatOutput>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(SubAgentStatOutput output) {
                        mView.showSubAgentStatView(output);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
